import Vue from 'vue'
// 在build 时  清除 alert
if (process.env.NODE_ENV !== 'development') {
    window.alert = function() {}
}
export default {
    install(Vue, options) {
        Vue.mixin({
            methods: {

            }
        })
        Vue.prototype.$ = {
            fmtDate: fmtDate,
            compileStr: compileStr, // 加密
            uncompileStr: uncompileStr, // 解密
            toast: toast, // toast
            isEmpty: isEmpty,
            cloneObj: cloneObj
        }
    }
}


/**
 * 格式化时间
 * hoog
 * @param {*} inputTime 时间戳
 */
function fmtDate(inputTime) {
    if (!inputTime) {
        return ''
    }
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

/**
 * 对字符串进行加密
 * hoog
 * @param {*} code 要加密的字符串
 */
function compileStr(code) { //对字符串进行加密         
    var c = String.fromCharCode(code.charCodeAt(0) + code.length);
    for (var i = 1; i < code.length; i++) {
        c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
    }
    return escape(c);
}

/**
 * 对加密的字符串进行解密
 * 需配合 comileStr 使用
 * hoog
 * @param {*} code 要解密的字符串
 */
function uncompileStr(code) {
    code = unescape(code);
    var c = String.fromCharCode(code.charCodeAt(0) - code.length);
    for (var i = 1; i < code.length; i++) {
        c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
    }
    return c;
}



//toast
let show = false

function toast(msg) {
    if (show == true) {
        return
    }
    show == true
    var el = document.createElement('div')
    var toast = document.body.appendChild(el)
    toast.innerHTML = msg
    toast.style.position = "fixed"
    toast.style.color = "#fff"
    toast.style.lineHeight = "1em"
    toast.style.display = "flex"
    toast.style.top = "50%"
    toast.style.left = "50%"
    toast.style.transform = "translate(-50% , -50%)"
    toast.style['background-color'] = "rgba(0, 0, 0, 0.4)"
    toast.style['border-radius'] = "4px"
    toast.style.padding = "1em"
    toast.style.zIndex = '30000000'
    setTimeout(function() {
        toast.style.display = "none"
        show = false
        document.body.removeChild(toast)
    }, 2000)
}



// 判断是否为空
function isEmpty(value) {
    if (value) {
        if (String(value).trim().length) {
            return false
        } else {
            return true
        }
    } else {
        return true
    }
}

// 深克隆
function cloneObj(value) {
    return JSON.parse(JSON.stringify(value))
}